﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UFSSWebApp
{
    public partial class friends : System.Web.UI.Page
    {
        UFSSservicereference.UserCreds uc = new UFSSservicereference.UserCreds();
        UFSSservicereference.UFSSserviceSoapClient ussc = new UFSSservicereference.UFSSserviceSoapClient();
        private UFSSservicereference.UFSSMessage[] message;

        protected void Page_Load(object sender, EventArgs e)
        {
            uc = (UFSSservicereference.UserCreds)Session["usercreds"];
            if (!IsPostBack)
                refresh();
        }

        private void refresh()
        {
            lstRequest.Items.Clear();
            string[] request = ussc.getRequestedFriends(uc).ToArray<string>();
            int requestCount = request.Length;
            lstRequest.Enabled = true;

            message = ussc.getMessages(uc);

            for (int i = 0; i < requestCount; i++)
                lstRequest.Items.Add(request[i]);

            if (lstRequest.Items.Count == 0)
            {
                lstRequest.Items.Add("No requests");
                lstRequest.Enabled = false;
            }

            lblMessages.Text = message.Length.ToString();
            lblError.Text = "";

            //gvUpdates.DataSource = 
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            try
            {
                ussc.acceptFriend(uc, lstRequest.SelectedItem.ToString());
                refresh();
            }
            catch
            {
                lblError.Text = "Error accepting friend.";
            }
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            try
            {
                ussc.rejectFriend(uc, lstRequest.SelectedItem.ToString());
                refresh();
            }
            catch
            {
                lblError.Text = "Error rejecting friend.";
            }
        }

        protected void btnViewProfile_Click(object sender, EventArgs e)
        {
            if (lstRequest.SelectedIndex != -1)
            {
                Session.Remove("user");
                Session.Add("user", lstRequest.SelectedItem.ToString());
                Response.Redirect("~/userpage.aspx");
            }
            else
                lblError.Text = "Please select a friend to view.";
        }

        protected void btnViewMessages_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/viewmessages.aspx");
        }   
    }
}